/**
 * GeoSOA/Spatialytics/mapstyles/contexts/DynamicThematicContext.js
 *
 * DynamicThematicContext
 *
 * Copyright (C) 2008-2009 GeoSOA Research Group, Universite Laval
 * See LICENSE file under this distribution for licensing information
 * 
 * @author Etienne Dube
 * @since 20090918
 **/

dojo.provide("GeoSOA.Spatialytics.mapstyles.contexts.DynamicThematicContext");
dojo.require("GeoSOA.Spatialytics.SOLAPContext");

/**
 * Dynamic thematic context object, for mixing in thematic context subclasses
 * which depend on data values from SOLAPContext (e.g. for behavior
 * such as dynamically defined intervals)
 * @param {Object} options Options passed to the constructor
 * 
 * @constructor
 */
dojo.declare("GeoSOA.Spatialytics.mapstyles.contexts.DynamicThematicContext", null, {
	
	constructor: function (options) {
	
		var options = options || {};
		
		/**
		 * {Object} options.solapContext (required)
		 */
		this.solapContext = options.solapContext;
	
	},
	
	/**
	 * Return the minimum and maximum values, along with the indexes of features
	 * having these values (first one if there are multiple features having the
	 * min or max value), from the passed features array.
	 * 
	 * @param {Array} features Array of features (OpenLayers.Vector.Feature)
	 * @param {String} valueField (optional) Name of the "value" field to be
	 * compared (defaults to "value" if unspecified)
	 * @return {Object} Object with min, max, minIdx and maxIdx fields. 
	 * @public
	 */
	calcMinMaxValues: function (features, valueField) {
		
		var theValueField = valueField || "value";
		
		var minmax = {
				min: null,
				max: null,
				minIdx: null,
				maxIdx: null
		};
		
		if (features.length > 0) {
			var feature = features[0];
			var value = feature.attributes[theValueField];
			if(value != null) {
				minmax.min = value;
				minmax.minIdx = 0;
				minmax.max = value;
				minmax.maxIdx = 0;
				var nbFeat = features.length;
				
				for (var i = 1 ; i < nbFeat ; i++) {
					feature = features[i];
					value = feature.attributes[theValueField];
					if (value < minmax.min) {
						minmax.min = value;
						minmax.minIdx = i;
					}
					if (value > minmax.max) {
						minmax.max = value;
						minmax.maxIdx = i;
					}
				}
			}
			else {
				// no value attribute!
				// TODO: throw something
			}
		}
			
		return minmax;
	}

});
